package com.coderman.infosys.auth.adapter.controller.system;

import com.coderman.infosys.auth.adapter.controller.BaseController;
import com.coderman.infosys.auth.adapter.convert.ModuleConverter;
import com.coderman.infosys.auth.domain.bo.system.SystemBO;
import com.coderman.infosys.auth.domain.factory.SystemFactory;
import com.coderman.infosys.plat.auth.common.dto.PageDTO;
import com.coderman.infosys.plat.auth.common.dto.ResultDataDto;
import com.coderman.infosys.plat.auth.common.page.PageBO;
import com.coderman.infosys.plat.auth.common.vo.PageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import com.coderman.infosys.auth.adapter.vo.system.request.UpdateModuleRequestVO;
import com.coderman.infosys.auth.adapter.vo.system.ModuleVO;
import com.coderman.infosys.auth.adapter.vo.system.request.CreateModuleRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.coderman.infosys.auth.domain.bo.system.ModuleBO;
import com.coderman.infosys.auth.domain.gataway.SystemModuleQueryGataWay;
import com.coderman.infosys.auth.domain.repository.SystemModuleRepository;


/**
* @Description:控制层
* @Author:
* @CreateTime:2022-01-04 15:27:39
* @version v1.0
*/
@RestController
public class ModuleController extends BaseController {
	
	protected Logger logger = LoggerFactory.getLogger(ModuleController.class);

	@Autowired
	private SystemModuleQueryGataWay systemModuleQueryGataWay;

	@Autowired
	private SystemModuleRepository systemModuleRepository;

	@Autowired
	private SystemFactory systemFactory;


	/**
	 *
	 * @Description 创建菜单信息
	 * @param createModuleRequestVO
	 * @return List<ModuleVO>
	 */
	@RequestMapping(value = "/module/createmodule")
	public ResultDataDto<Boolean> createModule(@RequestBody CreateModuleRequestVO createModuleRequestVO){
        ModuleBO moduleBO = ModuleConverter.INSTANCE.vo2bo(createModuleRequestVO);
		SystemBO systemBO = systemFactory.initSystemByModule(moduleBO);
		systemModuleRepository.registSystemMeta(systemBO);
		return ResultDataDto.success(true);
	}

	/**
	 *
	 * @Description 创建菜单信息
	 * @param updateModuleRequestVO
	 * @return List<ModuleVO>
	 */
	@RequestMapping(value = "/module/updatemodule")
	public ResultDataDto<Boolean> updateModule(@RequestBody UpdateModuleRequestVO updateModuleRequestVO){
        ModuleBO moduleBO = ModuleConverter.INSTANCE.vo2bo(updateModuleRequestVO);
		SystemBO systemBO = systemFactory.initSystemByModule(moduleBO);
		systemModuleRepository.updateSystemMeta(systemBO);
		return ResultDataDto.success(true);
	}

	/**
	 *
	 * @Description 获取系统菜单列表信息
	 * @param systemCode
	 * @return List<ModuleVO>
	 */
	@RequestMapping(value = "/module/getbysystemcode")
	public ResultDataDto<List<ModuleVO>> getBySystemCode(@RequestParam(value = "systemCode", required = true) String systemCode){
		List<ModuleBO> moduleBOList = systemModuleQueryGataWay.getModuleBySystemCode(systemCode);
		return ResultDataDto.success(ModuleConverter.INSTANCE.boList2voList(moduleBOList));
	}

	/**
	 *
	 * @Description 获取菜单信息
	 * @param moduleCode
	 * @return ModuleVO
	 */
	@RequestMapping(value = "/module/getbycode")
	public ResultDataDto<ModuleVO> getByModuleCode(@RequestParam(value = "moduleCode", required = true) String moduleCode){
        ModuleBO moduleBO = systemModuleQueryGataWay.getModuleByCode(moduleCode);
		ModuleVO moduleVO = ModuleConverter.INSTANCE.bo2VO(moduleBO);
		return ResultDataDto.success(moduleVO);
	}

	/**
	 *
	 * @Description 分页获取菜单信息
	 * @return PageDTO<ModuleVO>
	 */
	@RequestMapping(value = "/module/pagelist")
	public ResultDataDto<PageVO> getPageList(PageVO pageVO){
		PageBO pageBO = pageVO.convertToBO();
		List<ModuleBO> moduleBOList = systemModuleQueryGataWay.queryModulePage(pageBO);
		return ResultDataDto.success(pageVO.initData(ModuleConverter.INSTANCE.boList2voList(moduleBOList),pageBO.getTotalRows()));
	}

	/**
	 *
	 * 这里是走批量导入的场景接口，分两种情况，一种是针对单个系统的导入
	 * 第二种是针对于多个系统的多个菜单导入，所以需要一些数据预处理逻辑才能从系统上构建基于系统维度的聚合对象
	 * @Description 导入菜单信息
	 * @param createRequest
	 * @return Boolean
	 */
	@RequestMapping(value = "/module/import")
	public ResultDataDto<Boolean> importModuleList(@RequestBody List<CreateModuleRequestVO> createRequest){
		return null;
	}

	/**
	 *
	 * @Description 导出菜单信息
	 * @param systemCode
	 * @return Boolean
	 */
	@RequestMapping(value = "/module/export")
	public ResultDataDto<Boolean> exportModuleList(@RequestParam(value = "systemCode", required = true) String systemCode){

		return null;
	}
}
